﻿namespace Microsoft.SharePoint.Client
{
    using System;
    using System.ComponentModel;

    [ScriptType("SP.FileCollection", ServerTypeId="{d367b17c-170b-4691-a1e3-8bccf7686ce4}")]
    public class FileCollection : ClientObjectCollection<File>
    {
        [EditorBrowsable(EditorBrowsableState.Never)]
        public FileCollection(ClientRuntimeContext Context, ObjectPath ObjectPath) : base(Context, ObjectPath)
        {
        }

        [Remote]
        public File Add(FileCreationInformation parameters)
        {
            if (base.Context.ValidateOnClient)
            {
                if (parameters == null)
                {
                    throw ClientUtility.CreateArgumentNullException("parameters");
                }
                if (parameters != null)
                {
                    Uri uri;
                    if (parameters.Url == null)
                    {
                        throw ClientUtility.CreateArgumentNullException("parameters.Url");
                    }
                    if ((parameters.Url != null) && !Uri.TryCreate(parameters.Url, UriKind.RelativeOrAbsolute, out uri))
                    {
                        throw ClientUtility.CreateArgumentException("parameters.Url");
                    }
                    if ((parameters.Url != null) && (parameters.Url.Length < 1))
                    {
                        throw ClientUtility.CreateArgumentException("parameters.Url");
                    }
                    if (parameters.Content == null)
                    {
                        throw ClientUtility.CreateArgumentNullException("parameters.Content");
                    }
                }
            }
            File file = new File(base.Context, new ObjectPathMethod(base.Context, base.Path, "Add", new object[] { parameters }));
            file.Path.SetPendingReplace();
            ObjectIdentityQuery query = new ObjectIdentityQuery(file.Path);
            base.Context.AddQueryIdAndResultObject(query.Id, file);
            base.Context.AddQuery(query);
            base.AddChild(file);
            file.InitFromCreationInformation(parameters);
            return file;
        }

        [Remote]
        public File AddTemplateFile(string urlOfFile, TemplateFileType templateFileType)
        {
            if (base.Context.ValidateOnClient)
            {
                if (urlOfFile == null)
                {
                    throw ClientUtility.CreateArgumentNullException("urlOfFile");
                }
                if ((urlOfFile != null) && (urlOfFile.Length == 0))
                {
                    throw ClientUtility.CreateArgumentException("urlOfFile");
                }
                if (((templateFileType != TemplateFileType.StandardPage) && (templateFileType != TemplateFileType.WikiPage)) && (templateFileType != TemplateFileType.FormPage))
                {
                    throw ClientUtility.CreateArgumentException("templateFileType");
                }
            }
            File file = new File(base.Context, new ObjectPathMethod(base.Context, base.Path, "AddTemplateFile", new object[] { urlOfFile, templateFileType }));
            file.Path.SetPendingReplace();
            ObjectIdentityQuery query = new ObjectIdentityQuery(file.Path);
            base.Context.AddQueryIdAndResultObject(query.Id, file);
            base.Context.AddQuery(query);
            base.AddChild(file);
            return file;
        }

        protected override bool InitOnePropertyFromJson(string peekedName, JsonReader reader)
        {
            bool flag = base.InitOnePropertyFromJson(peekedName, reader);
            if (flag)
            {
                return flag;
            }
            return flag;
        }
    }
}

